package com.mjf.ys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.ys.domain.FsYsAllAuxiliaryBalanceQuery;
import com.mjf.ys.domain.FsYsAxiliaryItems;
import com.mjf.ys.domain.FsYsAxiliaryItemsItems;
import com.mjf.ys.domain.FsYsProductClass;
import com.mjf.ys.enums.YSApiEnum;
import com.mjf.ys.mapper.FsYsAllAuxiliaryBalanceQueryMapper;
import com.mjf.ys.service.FsYsAllAuxiliaryBalanceQueryService;
import com.mjf.ys.service.FsYsAxiliaryItemsItemsService;
import com.mjf.ys.service.FsYsAxiliaryItemsService;
import com.mjf.ys.util.YSSendUtil;
import com.mjf.ys.vo.AllAuxiliaryBalanceQueryVO;
import com.mjf.ys.vo.ProductClassVO;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/23 10:34
 */

@Slf4j
@Service
public class FsYsAllAuxiliaryBalanceQueryServiceImpl
    extends ServiceImpl<FsYsAllAuxiliaryBalanceQueryMapper, FsYsAllAuxiliaryBalanceQuery>
    implements FsYsAllAuxiliaryBalanceQueryService {

    @Autowired
    private YSSendUtil ysSendUtil;
    @Autowired
    private FsYsAxiliaryItemsService axiliaryItemsService;
    @Autowired
    private FsYsAxiliaryItemsItemsService axiliaryItemsItemsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncAccount(String startperiod) {

        List<AllAuxiliaryBalanceQueryVO.ContentVO> datas = new ArrayList<>();
        String sendUrl = ysSendUtil.getSendUrl(YSApiEnum.YS_API_AUXILIARY_BALANCE_QUERY_URL);
        Map<String, Object> resMap = new HashMap<>();
        log.info("同步科目辅助全景查询，开始 {} ", sendUrl);
        resMap.put("requestid", new Date().getTime());
        resMap.put("accbook_id", "2252973903497920516");
        resMap.put("startperiod", startperiod);
        resMap.put("endperiod", startperiod);
        resMap.put("currency", "standard");
        resMap.put("auxiliary", "");
        resMap.put("startaccsubject", "6612");
        resMap.put("endaccsubject", "6612.99");
        resMap.put("tally", "Y");
        List<FsYsAllAuxiliaryBalanceQuery> all = new ArrayList<>();
        List<FsYsAxiliaryItems> allItem = new ArrayList<>();
        List<FsYsAxiliaryItemsItems> allSubItem = new ArrayList<>();
        try {
            String repsp = ysSendUtil.reqForResp(sendUrl, resMap);
            System.out.println(repsp);
            // 解析接口
            AllAuxiliaryBalanceQueryVO classVO = JSON.parseObject(repsp, AllAuxiliaryBalanceQueryVO.class);
            if (classVO.getCode().equals("200")) {
                datas.addAll(classVO.getData().getList());
            } else {
                log.error("同步科目辅助全景查询失败，接口返回结果：{}", JSON.toJSONString(classVO));
            }
        } catch (Exception e) {
            log.error("同步科目辅助全景查询异常", e);
        }
        if (CollectionUtils.isNotEmpty(datas)) {
            for (AllAuxiliaryBalanceQueryVO.ContentVO vo : datas) {
                FsYsAllAuxiliaryBalanceQuery balanceQuery = new FsYsAllAuxiliaryBalanceQuery();
//                balanceQuery.setPeriod();
                balanceQuery.setAccsubjectId(vo.getAccsubject_id());
                balanceQuery.setAccsubjectCode(vo.getAccsubject_code());
                balanceQuery.setAccsubjectName(vo.getAccsubject_name());
                balanceQuery.setFirstaccsubjectName(vo.getFirstaccsubject_name());
                balanceQuery.setFirstaccsubjectCode(vo.getFirstaccsubject_code());
                balanceQuery.setCurrentperiodamtFcDebit(vo.getCurrentperiodamt_fc_debit());
                balanceQuery.setCurrentperiodamtOcDebit(vo.getCurrentperiodamt_oc_debit());
                balanceQuery.setCurrentperiodamtFcCredit(vo.getCurrentperiodamt_fc_credit());
                balanceQuery.setCurrentperiodamtOcCredit(vo.getCurrentperiodamt_oc_credit());
                balanceQuery.setAccumulationthisyearFcDebit(vo.getAccumulationthisyear_fc_debit());
//                balanceQuery.setAccumulationthisyearOcDebit(vo.getAccumulationthisyear_oc_debit());
                balanceQuery.setAccumulationthisyearFcCredit(vo.getAccumulationthisyear_fc_credit());
                balanceQuery.setAccumulationthisyearOcCredit(vo.getAccumulationthisyear_oc_credit());
                balanceQuery.setClosingbalanceFcDebit(vo.getClosingbalance_fc_debit());
                balanceQuery.setClosingbalanceOcDebit(vo.getClosingbalance_oc_debit());
//                balanceQuery.setClosingbalanceFcCredit(vo.getClosingbalance_fc_credit());
//                balanceQuery.setClosingbalanceOcCredit(vo.getClosingbalance_oc_credit());
                balanceQuery.setAccproperty(vo.getAccproperty());
                balanceQuery.setAccsubjectClass(vo.getAccsubject_class());
                balanceQuery.setTenantid(vo.getTenantid());
                balanceQuery.setDirection(vo.getDirection());
                balanceQuery.setCurrencyName(vo.getCurrency_name());
                balanceQuery.setCurrencyId(vo.getCurrency_id());
//                balanceQuery.setOpeningobalanceOcDebit(vo.getOpeningobalance_oc_debit());
//                balanceQuery.setOpeningobalanceFcDebit(vo.getOpeningobalance_fc_debit());
//                balanceQuery.setOpeningobalanceOcCredit(vo.getOpeningobalance_oc_credit());
//                balanceQuery.setOpeningobalanceFcCredit(vo.getOpeningobalance_fc_credit());
//                balanceQuery.setOpeningobalanceQtyDebit(vo.getOpeningobalance_qty_debit());
//                balanceQuery.setInternalCode(vo.getInternal_code());
                save(balanceQuery);
                if (CollectionUtils.isNotEmpty(vo.getAxiliaryItems())) {
                    for (AllAuxiliaryBalanceQueryVO.AxiliaryItemVO axiliaryItemVO : vo.getAxiliaryItems()) {
                        FsYsAxiliaryItems axiliaryItems = new FsYsAxiliaryItems();
                        axiliaryItems.setAllauxiliarybalancequeryid(balanceQuery.getId());
                        axiliaryItems.setAccsubjectId(balanceQuery.getAccsubjectId());
                        axiliaryItems.setAxiliaryname(axiliaryItemVO.getAxiliaryName());
                        axiliaryItems.setAxiliarycode(axiliaryItemVO.getAxiliaryCode());
                        axiliaryItemsService.save(axiliaryItems);
                        for (AllAuxiliaryBalanceQueryVO.ItemVO itemVO : axiliaryItemVO.getItems()){
                            FsYsAxiliaryItemsItems axiliaryItemsItems = new FsYsAxiliaryItemsItems();
                            axiliaryItemsItems.setAllauxiliarybalancequeryid(balanceQuery.getId());
                            axiliaryItemsItems.setAccsubjectId(balanceQuery.getAccsubjectId());
                            axiliaryItemsItems.setAxiliaryname(axiliaryItemVO.getAxiliaryName());
                            axiliaryItemsItems.setAxiliarycode(axiliaryItemVO.getAxiliaryCode());
                            axiliaryItemsItems.setItemscode(itemVO.getCustomer_code());
                            axiliaryItemsItems.setItemsid(itemVO.getCustomer_id());
                            axiliaryItemsItems.setItemsname(itemVO.getCustomer_name());
                            axiliaryItemsItemsService.save(axiliaryItemsItems);
                        }
                    }
                }
//                all.add(balanceQuery);
            }
            saveOrUpdateBatch(all);
        }
        log.info("商品同步结束,处理数据：{}", datas.size());
    }
}
